package com.qs.springboot.amqp.topic;

import com.qs.springboot.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.springframework.amqp.core.ExchangeTypes;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 生产者：指定exchange的类型为topic（允许使用* #通配指定routingKey）
 */
public class Send2 {

    private static final String exchange_name = "my_topic_test";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        //声明exchange，指定类型为topic
        channel.exchangeDeclare(exchange_name, ExchangeTypes.TOPIC);

        //发送消息
        String message = "新增用户：id=0001";
        channel.basicPublish(exchange_name, "user.insert", null, message.getBytes());
        System.out.println("【用户服务】send：" + message);

        String message2 = "删除用户：id=0001";
        channel.basicPublish(exchange_name, "user.delete", null, message2.getBytes());
        System.out.println("【用户服务】send：" + message2);

        //routingKey
        String message3 = "添加用户部门：id=0001";
        channel.basicPublish(exchange_name, "user.add.position", null, message3.getBytes());
        System.out.println("【用户服务】send：" + message3);

        channel.close();
        connection.close();
    }
}
